/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.songlanyun.config;

import com.songlanyun.modules.user.oauth2.OAuth2Filter;
import com.songlanyun.modules.user.oauth2.OAuth2Realm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 *
 * @author Mark sunlightcs@gmail.com
 */
@Configuration
public class ShiroConfig {

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    /**
     * shiro 拦截器
     *
     * @param securityManager
     * @return
     */
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();

        filterMap.put("/sys/login", "anon");

        // app状态
        filterMap.put("/sys/config/app/status", "anon");
        filterMap.put("/sys/config/app/update", "anon");

        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/admin/cnarea/lbs-refresh", "anon");
        filterMap.put("/admin/cnarea/test_child/**", "anon");

        filterMap.put("/api/**", "anon");
        filterMap.put("/**/test", "anon");
        filterMap.put("/**/export", "anon");
        filterMap.put("/**/import", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/admin/rights/kq/refund", "anon");


        //支付宝回调地址
        filterMap.put("/order/alipay_callback", "anon");
        filterMap.put("/order/wxpay_callback", "anon");
        filterMap.put("/yjPsv/balance_callback", "anon");
        filterMap.put("/yjPsv/wxpay_balancecallback", "anon");
        filterMap.put("/sysUserAgent/agentSuccess", "anon");
        filterMap.put("/sysUserAgent/wxagentSuccess", "anon");

        filterMap.put("/imgs/**", "anon");


        //图片地址过滤
        filterMap.put("/path/**", "anon");

        //第三方消息回调
        filterMap.put("/admin/supplychain/message/callback", "anon");

        // 区块链充值回调、提现回调
        filterMap.put("/chain/notice/**", "anon");

        filterMap.put("/**", "oauth2");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
